#!sh
# WMII Configuration
load std string regex expr echo

argv0 = $0

#mount -Aa {os dial unix!/tmp/ns.kris.:1/wmii >[1=0]} / || raise mount
#mount -Aa {styxmon {os rc -c 'dial $WMII_ADDRESS' >[1=0]}} / || raise mount
mount -Aa {os rc -c 'dial $WMII_ADDRESS' >[1=0]} / || raise mount

{`{read} && echo halt>/dev/sysctl}&

MODKEY=Mod1
UP=k
DOWN=j
LEFT=h
RIGHT=l

WMII_FONT=fixed
WMII_NORMCOLORS=('#222222' '#5FBF77' '#2A7F3F')
WMII_FOCUSCOLORS=('#ffffff' '#153F1F' '#2A7F3F')
WMII_BACKGROUND='#333333'

WMII_TERM=urxvt

fn wmiimenu {
	(nf nb nil sf sb nil) := ($WMII_NORMCOLORS $WMII_FOCUSCOLORS)
	os -d ${hostenv HOME} (
		dmenu -b -fn $WMII_FONT
		-nf $nf -nb $nb -sf $sf -sb $sb)
}

fn 9menu {
	os -d ${hostenv HOME} (
		wmii9menu -font ${hd ${split , $WMII_FONT}}
		-^(nf nb br)^$WMII_NORMCOLORS
		-^(sf sb br)^$WMII_FOCUSCOLORS $*)
}

# Column Rules
echo '/./ -> 60+40' >/colrules

# Tagging Rules
echo '
/Firefox/ -> www
/XMMS.*/ -> ~
/MPlayer.*/ -> ~
/.*/ -> sel
' >/tagrules

subfn seq {
	result=${expr $* seq}
}

subfn hostenv {
	arg := $"*
	result="{os rc -c 'echo -n $'^$arg </dev/null}
}

subfn ftl {
	result=${tl $*}
	result=$"result
}

subfn lines {
	ifs := "{echo}
	arg := $*
	result = `{$arg}
}

fn 'fn?' {
	args := $*
	~ ("{rescue '*' {} {whatis $args >[2]/dev/null}}
	   'load std; fn '*)
}

fn ifx {
	(pred fn val args) := $*
	if {$pred $val} {$fn $val $args}
}

fn dofn {
	ifx 'fn?' {$*} $*
}

fn run_command {
	os -b -d ${hostenv HOME} $* &
}

fn dprint {
	arg := $*
	or {~ $#debug 0} {~ $debug '' 0} { echo $arg }
}

subfn config_whatis {
	result=${lines {os rc -c 'PATH=$WMII_CONFPATH which $*' $* </dev/null} $*}
}

# Status Bar Info
fn status {
	echo ${re mg '[0-9]+\.[0-9]+' "{os uptime}} '|' `{date}
}

for(i in Key Event Action) {
	'{fn $i { fn '$i'-$1 ${tl $*} }}'
}

# Events
Event Start {
	if {~ $1 wmiirc} {
		rm -f $progs_file
		exit
	}
}

Event Key {
	dprint Key-$1
	Key-$1 $1
}

Event CreateTag { echo $WMII_NORMCOLORS $* > /lbar/$"* }
Event DestroyTag { rm /lbar/$"* }
Event FocusTag { echo $WMII_FOCUSCOLORS $* > /lbar/$"* }
Event UnfocusTag { echo $WMII_NORMCOLORS $* > /lbar/$"* }
Event UrgentTag { echo '*'${ftl $*} > /lbar/${ftl $*} }
Event NotUrgentTag { echo ${tl $*} > /lbar/${ftl $*} }

Event LeftBarClick {
	(button name) := $*
	if {~ $button 1} { echo view $name >/ctl }
}
Event LeftBarMouseDown {
	(button name) := $*
	if {~ $button 3} { echo view "{9menu ${lines read_tags}} >/ctl & }
}
lastcmd=''
Event ClientMouseDown {
	(client button) := $*
	if {~ $button 3} {
		lastcmd = `{9menu -initial $lastcmd Nop Delete Fullscreen}
		if{~ $#lastcmd 0} {lastcmd=''}
		cmp := {~ $lastcmd $*}
		if {$cmp Nop} {
		} {$cmp Delete} { echo kill >/client/$client/ctl
		} {$cmp Fullscreen} { echo Fullscreen toggle >/client/$client/ctl
		}
	}
}

# Actions
Action quit { echo quit >>/ctl }
Action rehash {
	flag x -
	proglist ${hostenv PATH} >$progs_file
}
Action status {
	flag x -
	if {rm /rbar/status >[2]/dev/null} { sleep 1 }
	echo $WMII_NORMCOLORS >/rbar/status
	while {status >/rbar/status} { sleep 1 }
}

ifx {ftest -x $*} {run $*} $home/.wmii-3.5/sh.wmii.local
fn Key { ifx {! 'fn?' $*} {fn $*} Key-$1 ${tl $*} }

fn Action {
	(action args) := $*
	or {dofn Action-$action $args} {
		ifx {! ~ $#* 0} {run_command $*} ${config_whatis $action} $args
	}
}

# Key Bindings
Key $MODKEY-Control-t {
	if { ~ `{wc -l /keys} 0 1} {
		initkeys
		echo grabmod $MODKEY >/ctl
	} {
		echo $MODKEY-Control-t >/keys
		echo grabmod Mod3 >/ctl
	}
}

Key $MODKEY-$LEFT { echo select left >/tag/sel/ctl }
Key $MODKEY-$RIGHT { echo select right >/tag/sel/ctl }
Key $MODKEY-$UP { echo select up >/tag/sel/ctl }
Key $MODKEY-$DOWN { echo select down >/tag/sel/ctl }

Key $MODKEY-Shift-$LEFT { echo send sel left >/tag/sel/ctl }
Key $MODKEY-Shift-$RIGHT { echo send sel right >/tag/sel/ctl }
Key $MODKEY-Shift-$DOWN { echo send sel down >/tag/sel/ctl }
Key $MODKEY-Shift-$UP { echo send sel up >/tag/sel/ctl }

Key $MODKEY-space { echo select toggle >/tag/sel/ctl }
Key $MODKEY-Shift-space { echo send sel toggle >/tag/sel/ctl }

Key $MODKEY-d { echo colmode sel default >/tag/sel/ctl }
Key $MODKEY-s { echo colmode sel stack >/tag/sel/ctl }
Key $MODKEY-m { echo colmode sel max >/tag/sel/ctl }

Key $MODKEY-f { echo Fullscreen toggle >/client/sel/ctl }

Key $MODKEY-Shift-c { echo kill >/client/sel/ctl }

Key $MODKEY-a { Action `{actionlist | wmiimenu} & }
Key $MODKEY-p { run_command rc -c "{wmiimenu <$progs_file} & }
Key $MODKEY-Return { run_command $WMII_TERM & }
Key $MODKEY-t { echo view `{read_tags | wmiimenu} >/ctl & }
Key $MODKEY-Shift-t {
	sel := "{cat /client/sel/ctl}
	read_tags | wmiimenu >/client/$sel/tags
}

Key $MODKEY-^${seq 0 9} { echo view ${tl ${splitr $1 -}} >/ctl }
Key Shift-$MODKEY-${seq 0 9} { echo ${tl ${splitr $1 -}} >/client/sel/tags}

# Functions
fn proglist {
	os find ${split : $"*} -maxdepth 1 -type f </dev/null | sed 's,.*/,,' | sort | uniq
	#for(d in /n/local^${split : $"*}) {
	#	fs filter {mode -d} $d
	#} | sed 's,.*/,,' | sort | uniq
}

fn getfuns {
	ls -p /env | sed -n 's/^fn-' ^ $1 ^ '-//p'
}

fn actionlist {
	{	rescue '*' {} {
			proglist ${hostenv WMII_CONFPATH}
		}
	 	getfuns Action
	} | sort | uniq
}

fn initkeys {
	getfuns Key >/keys
}

fn read_tags {
	ls -p /tag | grep -v '^sel$'
}

# WM Configuration
{
	echo grabmod $MODKEY
	echo border 2
	echo font $WMII_FONT
	echo focuscolors $WMII_FOCUSCOLORS
	echo normcolors $WMII_NORMCOLORS
} >/ctl

# Misc Setup
os xsetroot -solid $WMII_BACKGROUND </dev/null

dofn Local-Overrides

Action status &
progs_file=/tmp/proglist.${pid}
Action rehash &

# Tag Bar Setup
seltag=${lines sed 1q /tag/sel/ctl}
comm -13 ${pipe from {read_tags}} ${pipe from {ls -p /lbar/*}} |
	getlines { rm /lbar/$line }
read_tags | getlines {
	if {~ $line $seltag} {
		echo $WMII_FOCUSCOLORS $line
	} {
		echo $WMII_NORMCOLORS $line
	} >/lbar/$line
}

# Keygrab Setup
initkeys

echo Start wmiirc >/event

# Event Loop
getlines {
	(event args) := ${split ' 	' $line}
	dprint Event-$event: $args
	rescue '*' { dprint Exception: $exception } {
		dofn Event-$event $args
	} </dev/null
	dprint loop
} </event

